home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 83 / MacAddict_083_2003-07.iso / mac / Software / Development / VLC Source 0.5.3.dmg / src / interface / intf_eject.c < prev   
C/C++ Source or Header  |  2003-04-07  |  8KB  |  275 lines

  1. /*****************************************************************************
  2.  * intf_eject.c: CD/DVD-ROM ejection handling functions
  3.  *****************************************************************************
  4.  * Copyright (C) 2001, 2002 VideoLAN
  5.  * $Id: intf_eject.c,v 1.20 2003/03/24 17:15:30 gbazin Exp $
  6.  *
  7.  * Author: Julien Blache <jb@technologeek.org> for the Linux part
  8.  *               with code taken from the Linux "eject" command
  9.  *         Jon Lech Johansen <jon-vl@nanocrew.net> for Darwin
  10.  *         Gildas Bazin <gbazin@netcourrier.com> for Win32
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  25.  *****************************************************************************/
  26.  
  27. #include <vlc/vlc.h>
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. #ifdef HAVE_UNISTD_H
  33. #    include <unistd.h>
  34. #endif
  35.  
  36. #include <string.h>
  37.  
  38. #ifdef HAVE_FCNTL_H
  39. #   include <fcntl.h>
  40. #endif
  41.  
  42. #ifdef HAVE_DVD_H
  43. #   include <dvd.h>
  44. #endif
  45.  
  46. #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
  47. #   include <linux/version.h>
  48.     /* handy macro found in 2.1 kernels, but not in older ones */
  49. #   ifndef KERNEL_VERSION
  50. #       define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  51. #   endif
  52.  
  53. #   include <sys/types.h>
  54. #   include <sys/stat.h>
  55. #   include <sys/ioctl.h>
  56.  
  57. #   include <sys/ioctl.h>
  58. #   include <sys/mount.h>
  59.  
  60. #   include <linux/cdrom.h>
  61. #   if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
  62. #       include <linux/ucdrom.h>
  63. #   endif
  64.  
  65. #   include <scsi/scsi.h>
  66. #   include <scsi/sg.h>
  67. #   include <scsi/scsi_ioctl.h>
  68. #endif
  69.  
  70. #if defined( WIN32 ) && !defined( UNDER_CE )
  71. #   include <mmsystem.h>
  72. #endif
  73.  
  74. /*****************************************************************************
  75.  * Local prototypes
  76.  *****************************************************************************/
  77. #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
  78. static int EjectSCSI ( int i_fd );
  79. #endif
  80.  
  81. /*****************************************************************************
  82.  * intf_Eject: eject the CDRom
  83.  *****************************************************************************
  84.  * returns 0 on success
  85.  * returns 1 on failure
  86.  * returns -1 if not implemented
  87.  *****************************************************************************/
  88. int __intf_Eject( vlc_object_t *p_this, const char *psz_device )
  89. {
  90.     int i_ret = VLC_SUCCESS;
  91.  
  92. #ifdef SYS_DARWIN
  93.     FILE *p_eject;
  94.     char *psz_disk;
  95.     char sz_cmd[32];
  96.  
  97.     /*
  98.      * The only way to cleanly unmount the disc under MacOS X
  99.      * is to use the 'disktool' command line utility. It uses
  100.      * the non-public Disk Arbitration API, which can not be
  101.      * used by Cocoa or Carbon applications. 
  102.      */
  103.  
  104.     if( ( psz_disk = (char *)strstr( psz_device, "disk" ) ) != NULL &&
  105.         strlen( psz_disk ) > 4 )
  106.     {
  107. #define EJECT_CMD "/usr/sbin/disktool -e %s 0"
  108.         snprintf( sz_cmd, sizeof(sz_cmd), EJECT_CMD, psz_disk );
  109. #undef EJECT_CMD
  110.  
  111.         if( ( p_eject = popen( sz_cmd, "r" ) ) != NULL )
  112.         {
  113.             char psz_result[0x200];
  114.             i_ret = fread( psz_result, 1, sizeof(psz_result) - 1, p_eject );
  115.  
  116.             if( i_ret == 0 && ferror( p_eject ) != 0 )
  117.             {
  118.                 pclose( p_eject );
  119.                 return VLC_EGENERIC;
  120.             }
  121.  
  122.             pclose( p_eject );
  123.  
  124.             psz_result[ i_ret ] = 0;
  125.  
  126.             if( strstr( psz_result, "Disk Ejected" ) != NULL )
  127.             {
  128.                 return VLC_SUCCESS;
  129.             }
  130.         }
  131.     }
  132.  
  133.     return VLC_EGENERIC;
  134.  
  135. #elif defined(UNDER_CE)
  136.     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
  137.     return i_ret;
  138.  
  139. #elif defined(WIN32)
  140.     MCI_OPEN_PARMS op;
  141.     MCI_STATUS_PARMS st;
  142.     DWORD i_flags;
  143.     char psz_drive[4];
  144.  
  145.     memset( &op, 0, sizeof(MCI_OPEN_PARMS) );
  146.     op.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
  147.  
  148.     strcpy( psz_drive, "X:" );
  149.     psz_drive[0] = psz_device[0];
  150.     op.lpstrElementName = psz_drive;
  151.  
  152.     /* Set the flags for the device type */
  153.     i_flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID |
  154.               MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
  155.  
  156.     if( !mciSendCommand( 0, MCI_OPEN, i_flags, (unsigned long)&op ) ) 
  157.     {
  158.         st.dwItem = MCI_STATUS_READY;
  159.     /* Eject disc */
  160.         i_ret = mciSendCommand( op.wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0 );
  161.     /* Release access to the device */
  162.     mciSendCommand( op.wDeviceID, MCI_CLOSE, MCI_WAIT, 0 );
  163.     }
  164.     else i_ret = VLC_EGENERIC;
  165.  
  166.     return i_ret;
  167. #else   /* WIN32 */
  168.  
  169.     int i_fd;
  170.  
  171.     /* This code could be extended to support CD/DVD-ROM chargers */
  172.  
  173.     i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
  174.    
  175.     if( i_fd == -1 )
  176.     {
  177.         msg_Err( p_this, "could not open device %s", psz_device );
  178.         return VLC_EGENERIC;
  179.     }
  180.  
  181. #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
  182.     /* Try a simple ATAPI eject */
  183.     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
  184.  
  185.     if( i_ret != 0 )
  186.     {
  187.         i_ret = EjectSCSI( i_fd );
  188.     }
  189.  
  190.     if( i_ret != 0 )
  191.     {
  192.         msg_Err( p_this, "could not eject %s", psz_device );
  193.     }
  194.  
  195. #elif defined (HAVE_DVD_H)
  196.     i_ret = ioctl( i_fd, CDROMEJECT, 0 );
  197.  
  198. #else
  199.     msg_Warn( p_this, "CD-Rom ejection unsupported on this platform" );
  200.     i_ret = -1;
  201.  
  202. #endif
  203.     close( i_fd );
  204.  
  205.     return i_ret;
  206. #endif
  207. }
  208.  
  209. /* The following functions are local */
  210.  
  211. #if defined(SYS_LINUX) && defined(HAVE_LINUX_VERSION_H)
  212. /*****************************************************************************
  213.  * Eject using SCSI commands. Return 0 if successful
  214.  *****************************************************************************/
  215. static int EjectSCSI( int i_fd )
  216. {
  217.     int i_status;
  218.  
  219.     struct sdata
  220.     {
  221.         int  inlen;
  222.         int  outlen;
  223.         char cmd[256];
  224.     } scsi_cmd;
  225.  
  226.     scsi_cmd.inlen  = 0;
  227.     scsi_cmd.outlen = 0;
  228.     scsi_cmd.cmd[0] = ALLOW_MEDIUM_REMOVAL;
  229.     scsi_cmd.cmd[1] = 0;
  230.     scsi_cmd.cmd[2] = 0;
  231.     scsi_cmd.cmd[3] = 0;
  232.     scsi_cmd.cmd[4] = 0;
  233.     scsi_cmd.cmd[5] = 0;
  234.     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
  235.     if( i_status != 0 )
  236.     {
  237.         return VLC_EGENERIC;
  238.     }
  239.  
  240.     scsi_cmd.inlen  = 0;
  241.     scsi_cmd.outlen = 0;
  242.     scsi_cmd.cmd[0] = START_STOP;
  243.     scsi_cmd.cmd[1] = 0;
  244.     scsi_cmd.cmd[2] = 0;
  245.     scsi_cmd.cmd[3] = 0;
  246.     scsi_cmd.cmd[4] = 1;
  247.     scsi_cmd.cmd[5] = 0;
  248.     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
  249.     if( i_status != 0 )
  250.     {
  251.         return VLC_EGENERIC;
  252.     }
  253.   
  254.     scsi_cmd.inlen  = 0;
  255.     scsi_cmd.outlen = 0;
  256.     scsi_cmd.cmd[0] = START_STOP;
  257.     scsi_cmd.cmd[1] = 0;
  258.     scsi_cmd.cmd[2] = 0;
  259.     scsi_cmd.cmd[3] = 0;
  260.     scsi_cmd.cmd[4] = 2;
  261.     scsi_cmd.cmd[5] = 0;
  262.     i_status = ioctl( i_fd, SCSI_IOCTL_SEND_COMMAND, (void *)&scsi_cmd );
  263.     if( i_status != 0 )
  264.     {
  265.         return VLC_EGENERIC;
  266.     }
  267.   
  268.     /* Force kernel to reread partition table when new disc inserted */
  269.     i_status = ioctl( i_fd, BLKRRPART );
  270.   
  271.     return i_status;
  272. }
  273. #endif
  274.  
  275.